<?php
/**
 * Created by PhpStorm.
 * User: leixiaotian
 * Date: 2020/9/22
 * Time: 11:17
 */

namespace App\Model;

use Illuminate\Support\Facades\DB;

class CouplingRing extends BaseModel
{
    protected $connection = 'api';
    protected $table = 'coupling_ring';
    protected $fields = ['ring_id as ringId', 'name as ringName', 'image', 'gray_image as grayImage','type', 'svg',
        'gray_svg as graySvg', 'ring_desc as ringDesc', 'is_show as isShow',
        'is_clear as isClear', 'sort', 'level','min_value','max_value'];

    /**
     * 创建戒指
     * @param $data
     */
    public function createRing($data)
    {
        $insertData['name'] = $data['name'];
        $insertData['image'] = $data['data_image'];
        $insertData['gray_image'] = $data['data_image'];
        $insertData['svg'] = $data['svg'];
        $insertData['gray_svg'] = $data['svg'];
        $insertData['type'] = 1;    //荣誉戒指
        return curl_request(getKkaudioApiUrl() . '/inner/prop/ring/createOrUpdate', $insertData, 'json', 'POST');
//        return DB::connection($this->connection)->table($this->table)->insert($insertData);
    }

    /**
     * 编辑戒指
     * @param $id
     * @param $data
     */
    public function editRing($id, $data)
    {
        unset($data['gift_id']);
        $data['gray_image'] = $data['image'];

        $data['svg'] = $data['svg'];
        if(isset($data['data_image'])) {
            $data['image'] = $data['data_image'];
            unset($data['data_image']);
        }
        return curl_request(getKkaudioApiUrl() . '/inner/prop/ring/createOrUpdate', $data, 'json', 'POST');

//        return DB::connection($this->connection)->table($this->table)
//            ->where('ring_id', '=', $id)
//            ->update($data);
    }

    public function getRingList ($offset, $rows = 20, $category = null, $name = '', $isShow = -1) {
        $condition1 = [];
        $condition2 = [];
        $condition3 = [];
        $query = DB::connection($this->connection)->table($this->table);
        if ($category !== null) {
            $condition1['type'] = $category;
            $query->where($condition1);
        }
        if ($isShow != -1) {
            $condition3['is_show'] = $isShow;
            $query->where($condition3);
        }
        if (!empty($name)) {
            $condition2['name'] = $name;
            $query->where($condition2);
        }

        return $query->offset($offset)->limit($rows)->select($this->fields)->get();
    }

    public function getRingListCount ($category = null, $name = '',$isShow ='') {
        $condition1 = [];
        $condition2 = [];
        $condition3 = [];
        $query = DB::connection($this->connection)->table($this->table);
        if ($category !== null) {
            $condition1['type'] = $category;
            $query->where($condition1);
        }
        if ($isShow != -1) {
            $condition3['is_show'] = $isShow;
            $query->where($condition3);
        }
        if (!empty($name)) {
            $condition2['name'] = $name;
            $query->where($condition2);
        }

        return $query->count();
    }
}


